<html>
<head><meta charset="utf-8"><title>Is ref-cast actually safe? · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20ref-cast.20actually.20safe.3F.html">Is ref-cast actually safe?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="199092973"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20ref-cast%20actually%20safe%3F/near/199092973" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20ref-cast.20actually.20safe.3F.html#199092973">(May 28 2020 at 22:15)</a>:</h4>
<p>I stumbled across the following crate: <a href="https://crates.io/crates/ref-cast">https://crates.io/crates/ref-cast</a> by <span class="user-mention" data-user-id="119235">@David Tolnay</span> .<br>
and it made me think, strictly memory wise, sure the pointers are equivalent. but is it actually safe?</p>
<p>Consider the discussions here: <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Constructing.20ZSTs">https://rust-lang.zulipchat.com/#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Constructing.20ZSTs</a> there are a lot of examples of type that require the constructor being called for them to be safe to use, either because of Drop impl(which isn't a problem for ref-cast because of a reference) or functions on that type.</p>
<p>I can definitely imagine types that their constructor have side effects(asm/C calls / writing to statics) that without makes any functions on the type be UB.<br>
Something like: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=3a55c119fb12370e30f95645edd23464">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=3a55c119fb12370e30f95645edd23464</a></p>



<a name="199093265"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20ref-cast%20actually%20safe%3F/near/199093265" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20ref-cast.20actually.20safe.3F.html#199093265">(May 28 2020 at 22:19)</a>:</h4>
<p>I did not consider the fact that it only gives a derive macro, meaning that the implementer of the struct provides this and no one else.</p>
<p>So if he derives it he needs to make sure all the invariants still hold.</p>



<a name="199094283"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20ref-cast%20actually%20safe%3F/near/199094283" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20ref-cast.20actually.20safe.3F.html#199094283">(May 28 2020 at 22:30)</a>:</h4>
<blockquote>
<p>So if he derives it he needs to make sure all the invariants still hold.</p>
</blockquote>
<p>basically this</p>



<a name="199094308"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20ref-cast%20actually%20safe%3F/near/199094308" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20ref-cast.20actually.20safe.3F.html#199094308">(May 28 2020 at 22:31)</a>:</h4>
<p>casting an arbitrary reference to an arbitrary other reference whose type has the same layout wouldn't be fine</p>



<a name="199094620"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20ref-cast%20actually%20safe%3F/near/199094620" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20ref-cast.20actually.20safe.3F.html#199094620">(May 28 2020 at 22:35)</a>:</h4>
<p>Always amazes me how I understand things 2-5 minutes after I post a question <span aria-label="face palm" class="emoji emoji-1f926" role="img" title="face palm">:face_palm:</span>‍♂️</p>



<a name="199095613"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20ref-cast%20actually%20safe%3F/near/199095613" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20ref-cast.20actually.20safe.3F.html#199095613">(May 28 2020 at 22:47)</a>:</h4>
<p>It's one of the classic Rust blunders, right up there with "never get involved with a land war in Asia".</p>



<a name="199246940"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20ref-cast%20actually%20safe%3F/near/199246940" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20ref-cast.20actually.20safe.3F.html#199246940">(May 30 2020 at 09:08)</a>:</h4>
<p><span class="user-mention silent" data-user-id="249222">Elichai Turkel</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Is.20ref-cast.20actually.20safe.3F/near/199094620">said</a>:</p>
<blockquote>
<p>Always amazes me how I understand things 2-5 minutes after I post a question <span aria-label="face palm" class="emoji emoji-1f926" role="img" title="face palm">:face_palm:</span>‍♂️</p>
</blockquote>
<p>Happens to me all the time :)</p>



<a name="199246953"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20ref-cast%20actually%20safe%3F/near/199246953" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20ref-cast.20actually.20safe.3F.html#199246953">(May 30 2020 at 09:08)</a>:</h4>
<p>that's why asking the question to a rubber duck actually works^^</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>